<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var minInsertions = function (s) {
      // 左下到右上，保证计算dp[i][j]时，dp[i + 1][j]、dp[i][j - 1]均已计算过
      let len = s.length;
      let dp = new Array(len).fill(0).map(() => new Array(len).fill(0));
      for (let i = len - 1; i >= 0; i--) {
        for (let j = i + 1; j <= len - 1; j++) {
          if (s[i] === s[j]) {
            dp[i][j] = dp[i + 1][j - 1];
          } else {
            dp[i][j] = Math.min(dp[i][j - 1], dp[i + 1][j]) + 1;
          }
        }
      }
      return dp[0][len - 1];
    };
    console.log(minInsertions("zzazz"));
  </script>
</body>

</html>